package cn.wlh.learn.webmagic.pipeline;

import cn.hutool.core.date.CalendarUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.wlh.learn.webmagic.tool.AliOssClientTool;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.Calendar;
import java.util.Collection;
import java.util.Map;

/**
 * 返回oss路径规则:
 * <pre>
 *         // 设置公共读  = >   https://bucket-name.oss-cn-beijing.aliyuncs.com/mulu/aaa.jpg
 *         if(this.publicRead){
 *             //
 *             return aliOssClientTool.getExtranetEndpoint() + '/' + path;
 *         }else {
 *             Calendar now = CalendarUtil.calendar();
 *             // 设置了有效的过期时间  =>  https://bucket-name.oss-cn-beijing.aliyuncs.com/mulu/aaa.jpg?秘钥*****一大串
 *             if ( expiration!=null  && CalendarUtil.compare(now,expiration)  == -1){
 *                 return aliOssClientTool.getPresignedUrl(path,expiration);
 *             }else{
 *                 // 没有设置过期时间，或者过期时间比现在早   => mulu/aaa.jpg
 *                 return path;
 *             }
 *         }
 * </pre>
 */
public class AliOssPipeline extends AdapterDownLoadConversoinPipeline {
    private final static Log logger = LogFactory.get();
    /**
     *  阿里oss工具类
     */
    private final AliOssClientTool aliOssClientTool;
    /**
     * oss上传的目录。
     */
    private final String storePath;
    /**
     * 过期时间。
     */
    private Calendar expiration;
    /**
     * 公共读。
     * 带地址,外网域名;
     */
    private boolean publicRead;
    /**
     * 公共读。
     * 带地址,外网域名;
     */
    private String showEndpointKey;
    /**
     * 设置读取和存入的key
     * @param from
     * @param to
     * @param aliOssClientTool
     * @param storePath
     */
    public AliOssPipeline( String from, String to, AliOssClientTool aliOssClientTool,String storePath) {
        super(from,to);
        this.aliOssClientTool = aliOssClientTool;
        this.storePath = storePath;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        super.process(resultItems, task);
        if (StrUtil.isNotBlank(this.showEndpointKey)) {
            resultItems.put(this.showEndpointKey,aliOssClientTool.getExtranetEndpoint());
        }
    }

    @Override
    protected Object downLoadStore(InputStream inputStream, String fileName) throws Exception {
        // 上传oss，获得简单的相对路径。
        String path = aliOssClientTool.uploadObjectOSS(this.storePath, fileName, inputStream);

        // 设置公共读  = >   https://bucket-name.oss-cn-beijing.aliyuncs.com/mulu/aaa.jpg
        if(this.publicRead){
            //
            return aliOssClientTool.getExtranetEndpoint() + '/' + path;
        }else {
            Calendar now = CalendarUtil.calendar();
            // 设置了有效的过期时间  =>  https://bucket-name.oss-cn-beijing.aliyuncs.com/mulu/aaa.jpg?秘钥*****一大串
            if ( expiration!=null  && CalendarUtil.compare(now,expiration)  == -1){
                return aliOssClientTool.getPresignedUrl(path,expiration);
            }else{
                // 没有设置过期时间，或者过期时间比现在早   => mulu/aaa.jpg
                return path;
            }
        }
    }

    public void setExpiration(Calendar expiration) {
        this.expiration = expiration;
    }

    public void setPublicRead(boolean publicRead) {
        this.publicRead = publicRead;
    }

    public void setShowEndpointKey(String showEndpointKey) {
        this.showEndpointKey = showEndpointKey;
    }

    public static void main(String[] args) {

        Calendar now = CalendarUtil.calendar();
        ThreadUtil.sleep(111);
        Calendar calendar = CalendarUtil.calendar();


        System.out.println(CalendarUtil.compare(calendar, now));

        System.out.println(CalendarUtil.compare(now, calendar));

        System.out.println(CalendarUtil.compare(now, null));

        System.out.println(CalendarUtil.compare(null, now));


    }
}